/**
 * Copyright (C) 2007 Mustafa K. Isik - isik@acm.org
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

package com.paxcodi.rmi.fibonacci.client;

import java.math.BigInteger;
import java.net.*;
import java.rmi.*;

import com.paxcodi.rmi.fibonacci.server.*;

public class FibonacciClient {

    /**
     * @param args hostname of server running rmi registry - format <code>rmi://host.domain:port/fibonacci</code>
     */
    public static void main(String[] args) {
	if (args.length == 0 || !args[0].startsWith("rmi://")) {
	    System.err.println("missing or malformed parameter - usage:\n" +
		    "java FibonacciClient rmi://host.domain:port/fibonacci (number)*\n" + 
	    "you may omit the port no in which case the standard port 1099 will be addressed");
	    return;
	}

	try{
	    Object lookupObj = Naming.lookup(args[0]); // getting fibonacci stub from server rmiregistry
	    Fibonacci remoteCalculator = (Fibonacci) lookupObj; //restoring lost type information
	    for (int i = 1; i < args.length; i++) {
		try{
		    System.out.println("Fibonacci Number " + (Integer.parseInt(args[i]) + 1) + " is " + remoteCalculator.getFibonacci(new BigInteger(args[i])));
		} catch(NumberFormatException ne) {
		    System.err.println(args[i] + " is not an integer.");
		} 
	    }
	} catch(MalformedURLException e) {
	    System.err.println(args[0] + " is not a valid RMI URL, see exception " + e);
	} catch (RemoteException e) {
	    System.err.println("Exception thrown by remote object: " + e);
	} catch (NotBoundException e) {
	    System.err.println("Could not find the requested object on the server: " + e);
	}
    }

}
